
package DAO;


import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.*;
import DTO.ShippingMethod;
import Exception.*;

/**
 * This class represents abstraction for data access from shipping table in database.
 * This class is called from DAO as a proxy.
 * @see DAO
 * @Date Mar 30, 2011
 * @author Tony Lei
 */
public class ShippingDAO {

    private Connection conn;
    private static final Logger logger = Logger.getLogger(PromoDAO.class.getName());

     /**
     * constructor for this class.
     * @param conn connection object
     */
    public ShippingDAO(Connection conn) {
        this.conn = conn;
    }

   /**
     * This method retrieves shipping method associated with a province.
     * @param province a string
     * @return a list of Shipping Method data transfer objects
     * @throws {@link KwickKioskDBException}
     */
    public ArrayList<ShippingMethod> getShippingMethodList(String province)
            throws KwickKioskDBException {
        ArrayList<ShippingMethod> list = new ArrayList<ShippingMethod>();
        Statement sqlStatement;
        try {
            sqlStatement = conn.createStatement();
            String sqlText = "select * "
                    + "from shippingmethod "
                    + "where area='" + province + "'";
            System.out.println(sqlText);
            ResultSet rs = sqlStatement.executeQuery(sqlText);
            
            while (rs.next()) {

                list.add(new ShippingMethod(rs.getInt(1),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getInt(4),
                        rs.getInt(5),
                        rs.getDouble(6)));
            }
            sqlStatement.close();
        } catch (SQLException ex) {
            logger.log(Level.SEVERE, null, "ShippingDAO : getShippingMethodList" + ex);
            throw new KwickKioskDBException("ShippingDAO : getShippingMethodList",
                    ex.getMessage());
        }
        return list;
    }
}
